home *** CD-ROM | disk | FTP | other *** search
- /* * Commlib source * */
- /* ssg Commlib */
-
- /* Copyright © 1986,87 by small systems guild. All rights reserved. */
-
- /* Written to compile under Aztec C version 1.06h */
- /* and under Lightspeed C version 2.01 */
-
- #include <extcomm.h>
-
- Boolean CheckPort(whichPort) /* take care of data arriving at comm port */
- TermData *whichPort;
- {
- Rect visRect;
- long count = NULL;
- char inChar;
-
- SerGetBuf(whichPort->commPort->refin,&count);/* check current port status */
-
- if (count < 1) /* if no characters waiting at port */
- return(FALSE); /* return immediately with FALSE */
-
- SetPort(whichPort->commWindow);
- CalcContentRect(whichPort->commWindow,&visRect);
- ClipRect(&visRect);
- if (count > 0) { /* if characters waiting at port */
- getu(whichPort->commPort,&inChar); /* read next character from port */
- scrput(whichPort,inChar); /* write it to the screen */
- count--; /* decrement count by one character */
- }
- return(TRUE);
- }
-
- void CommKey(aWindow,keyChar,modifiers)
- WindowPtr aWindow;
- char keyChar;
- int modifiers;
- {
- unsigned char outChar;
- char str[16];
-
- outChar = (unsigned char)keyChar; /* char sent MAY equal char typed if*/
- /* none of the cases below are true */
- if ((modifiers & cmdKey) && (outChar > 96) && (outChar < 123)) { outChar -= 97;
- outChar -= 96;
- }
-
- switch (outChar) { /* handle special characters typed */
- case '`': /* if possible mac escape character */
- if (!(modifiers & cmdKey)) /* if command key was not held down */
- outChar = ESC; /* set outChar equal to escape char */
- break;
-
- case BS: /* if it was a backspace (control H)*/
- outChar = DEL; /* change it to a delete character */
- break;
- }
- putu(((TermData *)(GetWPort(aWindow)))->commPort,outChar); /* send character out the comm port */
- }
-
- OSErr putu(thePort,outChar) /* send a character out the comm port */
- SPortPtr thePort;
- char outChar;
- {
- long numChars = 1;
-
- return(FSWrite(thePort->refout,&numChars,&outChar));
- }
-
- OSErr getu(thePort,theChar) /* get a character from the comm port */
- SPortPtr thePort;
- char *theChar;
- {
- char inChar = 0;
- long numChars = 1;
- OSErr result;
-
- result = FSRead(thePort->refin,&numChars,&inChar);
- *theChar = inChar;
- return(result);
- }
-
- OSErr setupport(whichPort) /* init communications port to active status */
- SerialPort *whichPort;
- {
- if (ValidPointer((Ptr)whichPort))
- if (ValidPointer((Ptr)(whichPort->refin))) {
- SerReset(whichPort->refin,whichPort->baud |
- whichPort->parity | whichPort->stopbits |
- whichPort->databits);
- return(noErr);
- }
- return(nilHandleErr);
- }
-
- void ResetPort(whichPort) /* reset communications port to inactive status */
- SerialPort *whichPort;
- {
- if (ValidPointer((Ptr)whichPort))
- if (ValidPointer((Ptr)(whichPort->refin)))
- SerSetBuf(whichPort->refin,NULL,0);
- }
-
- OSErr InitSPort(whichPort,which) /* init ports and set to indicated one */
- SerialPort *whichPort;
- char which;
- {
- static char inbuf[2048];
-
- if (!ValidPointer((Ptr)whichPort))
- return(nilHandleErr);
-
- switch(which) {
- case 'A':
- case 'a':
- whichPort->out = "\P.aout";
- whichPort->in = "\P.ain";
- break;
- case 'B':
- case 'b':
- whichPort->out = "\P.bout";
- whichPort->in = "\P.bin";
- break;
- default:
- return(nilHandleErr);
- }
- whichPort->baud = baud1200;
- whichPort->parity = noParity;
- whichPort->stopbits = stop20;
- whichPort->databits = data8;
- OpenDriver(whichPort->out, &(whichPort->refout));
- OpenDriver(whichPort->in, &(whichPort->refin));
- setupport(whichPort);
- SerSetBuf(whichPort->refin,inbuf,2048);
- }
-
- void scrput(cWD,c) /* output a character to the window with */
- TermData *cWD; /* escape sequence expansion and emulation */
- char c;
- {
- register int i;
-
- switch(cWD->escFlag) {
- case 0:
- switch(c) {
- case ESC:
- cWD->escFlag = 1;
- RETURN;
- case LINEFEED:
- InvertRect(&(cWD->cursRect));
- if (cWD->row < (cWD->visRows - 1))
- cWD->row++;
- else
- del_lin(cWD,0);
- break;
- case CR:
- EraseRect(&(cWD->cursRect));
- cWD->charBuf[cWD->row][cWD->col] = c;
- /*(cWD->row)++*/;
- cWD->col = 0;
- break;
- case BS:
- InvertRect(&(cWD->cursRect));
- if (cWD->col)
- cWD->col--;
- else {
- cWD->col = cWD->visCols - 1;
- if (cWD->row)
- cWD->row--;
- else
- cWD->row = cWD->visRows - 1;
- }
- break;
- case STD_RIGHTARROW :
- InvertRect(&(cWD->cursRect));
- if (cWD->col < cWD->visCols)
- cWD->col++;
- else {
- cWD->col = 0;
- if (cWD->row < cWD->visRows - 1)
- cWD->row++;
- else
- cWD->row = 0;
- }
- break;
- case HOMECURSOR:
- InvertRect(&(cWD->cursRect));
- cWD->col = cWD->row = 0;
- /*cWD->firstIdx = 0;*/
- for (i = 0; i < cWD->visRows; i++)
- cWD->charBuf[i][0] = (unsigned char)0;
- break;
- case TAB:
- c = ((cWD->col + 4) & ~3) - cWD->col;
- while(c--)
- scrput(cWD,' ');
- return;
- case CLEARSCREEN:
- EraseRect(&(cWD->commWindow->portRect));
- cWD->col = cWD->row = 0;
- /*cWD->firstIdx = 0;*/
- for (i = 0; i < cWD->visRows; i++)
- cWD->charBuf[i][0] = (unsigned char)0;
- break;
- case STD_UPARROW:
- InvertRect(&(cWD->cursRect));
- if (cWD->row > 0)
- cWD->row--;
- else
- cWD->row = cWD->visRows - 1;
- break;
- default:
- if (c < 0x20 || c >= 0x7f)
- return;
- EraseRect(&(cWD->cursRect));
- MoveTo((cWD->col*6)+1, (cWD->row*12)+9);
- cWD->charBuf[cWD->row][cWD->col] = c;
- DrawChar(c);
- if (cWD->col < cWD->visCols - 1)
- cWD->col++;
- else {
- cWD->col = 0;
- if (cWD->row < cWD->visRows)
- cWD->row++;
- else
- del_lin(cWD,0);
- }
- break;
- }
- if (cWD->row > cWD->visRows - 1) {
- cWD->row = cWD->visRows - 1;
- /*cWD->firstIdx++;*/
- }
- break;
- case 1:
- RETURN;
- break; /* throw away character after escape for now */
- /* terminal emulation hook goes here and default*/
- }
- cWD->cursRect.left = cWD->col * 6;
- cWD->cursRect.top = cWD->row * 12;
- cWD->cursRect.right = cWD->cursRect.left + 7;
- cWD->cursRect.bottom = cWD->cursRect.top + 13;
- InvertRect(&(cWD->cursRect));
- }
-
- void del_lin(cWD,lin) /* delete line primitive */
- TermData *cWD;
- int lin;
- {
- Rect rect;
- int i;
-
- rect.left = 0;
- rect.right = cWD->commWindow->portRect.right - 15;
- rect.top = lin * 12;
- rect.bottom = (cWD->visRows) * 12;
- ScrollRect(&rect,0,-12,cWD->updateRgn);
- if (lin <= cWD->row)
- (cWD->row)--;
- /* cWD->firstIdx++;*/
- for (i = 0; i < 80; i++)
- cWD->charBuf[cWD->row][i] = (unsigned char)0;
- }
-
- void ins_lin(cWD,lin) /* insert blank line primitive */
- TermData *cWD;
- int lin;
- {
- Rect rect;
-
- rect.left = 0;
- rect.right = cWD->commWindow->portRect.right - 15;
- rect.top = lin * 12;
- rect.bottom = (cWD->visRows - 1) * 12;
- ScrollRect(&rect,0,12,cWD->updateRgn);
- }
-
- void clr_eol(cWD) /* clear to end of line primitive */
- TermData *cWD;
- {
- Rect rect;
-
- rect.left = cWD->col * 6;
- rect.top = cWD->row * 12;
- rect.right = cWD->commWindow->portRect.right - 15;
- rect.bottom = rect.top + 13;
- EraseRect(&rect);
- }
-
- void ins_char(cWD) /* insert character primitive */
- TermData *cWD;
- {
- Rect rect;
-
- rect.left = cWD->col * 6;
- rect.top = cWD->row * 12;
- rect.right = cWD->commWindow->portRect.right - 15;
- rect.bottom = rect.top + 13;
- ScrollRect(&rect,6,0,cWD->updateRgn);
- }
-
- void del_char(cWD) /* delete character primitive */
- TermData *cWD;
- {
- Rect rect;
-
- rect.left = cWD->col * 6;
- rect.top = cWD->row * 12;
- rect.right = cWD->commWindow->portRect.right - 15;
- rect.bottom = rect.top + 13;
- ScrollRect(&rect,-6,0,cWD->updateRgn);
- }
-
- void StdCommMenus(baudMenu,portMenu)
- MenuHandle *baudMenu,*portMenu;
- {
- if (ValidPointer((Ptr)baudMenu)) {
- *baudMenu = NewMenu(BAUDmenuID,"\PBaud");
- AppendMenu(*baudMenu,"\P 300; 600;1200;1800;2400;3600;4800;7200;9600;19200;57600");
- InsertMenu(*baudMenu,0);
- }
- if (ValidPointer((Ptr)portMenu)) {
- *portMenu = NewMenu(PORTmenuID,"\PPort");
- AppendMenu(*portMenu,"\PModem Port;Printer Port");
- InsertMenu(*portMenu,0);
- }
- DrawMenuBar();
- }
-
- OSErr SendStr(whichPort,linePtr) /* send Pascal string out comm port */
- SerialPort *whichPort;
- char *linePtr;
- {
- long count;
- int i;
-
- for (i = 1;i <= linePtr[0];i++)
- putu(whichPort,linePtr[i]);
- putu(whichPort,CR);
- /* alternative faster algorithm! */
- /* count = linePtr[0];
- FSWrite(whichPort->refout,&count,linePtr);*/
- }
-
- OSErr RecvStr(whichPort,linePtr)
- SerialPort *whichPort;
- char *linePtr;
- {
- EventRecord evt;
- long count,endTicks;
- char inChar;
- int mask,charTot = 0,patience = 0;
- OSErr result;
-
- mask = keyDownMask | autoKeyMask | mDownMask;
- GetNextEvent(mask,&evt);
- endTicks = evt.when + patience;
-
- do {
- count = NULL;
- SerGetBuf(whichPort->refin,&count);
-
- /* if (charTot + count > linePtr[0])
- count = linePtr[0] - charTot;
- result = FSRead(whichPort->refin,&count,&linePtr);
- scrLine(whichPort,linePtr,(int)count);
- charTot += count;*/ /* prototype faster algorithm! */
-
- while (count > 0) {
- getu(whichPort,&inChar);
- if (inChar == CR || inChar == LINEFEED || charTot == linePtr[0]) {
- linePtr[0] = charTot;
- return(0); /* TRUE */
- }
- charTot++;
- count--;
- linePtr[charTot] = inChar;
- if (GetNextEvent(mask,&evt))
- return(-1); /* FALSE */
- if (evt.when > endTicks)
- return(-1); /* FALSE */
- }
- } while (!GetNextEvent(mask,&evt) && evt.when < endTicks);
- return(-1); /* FALSE */
- }
-
- Boolean Logon(whichPort,linePtr,patience)
- TermData *whichPort;
- char *linePtr;
- int patience;
- {
- return(TRUE);
- }
-